﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using DatabaseLib;
using ABMath.ModelFramework.Data;
using MathNet.Numerics.LinearAlgebra;
using ABMath.ModelFramework.Transforms;
using ABMath.ModelFramework.Models;
using System.IO;
using GisEntity;
namespace TestModel
{
    class Program
    {
        //private string path = "c:\\table.csv";
        static void Main(string[] args)
        {
            //using (HnMapEntities database = new HnMapEntities()) 
            //{
            //    var queryResult = from item in database.GDP
            //                      where item.CITYNAME.Equals("长沙市")
            //                      select item;
            //    foreach (var item in queryResult.ToList())
            //    {
            //        Console.WriteLine("{0} is : {1}", item.TIME.ToString().Substring(0,4), item.GDPVALUE);
            //    }
            //}
            using (GISService.ServiceClient client = new GISService.ServiceClient())
            {
                List<GISService.MyTimeSeries> TSList = client.GetTimeSeriesList().ToList();
                foreach (var item in TSList)
                {
                    for (int i = 0; i < item.Times.Count(); i++)
                    {
                        Console.WriteLine("{0} in {1} is: {2}", item.Titel, item.Times.GetValue(i).ToString().Substring(0, 4), item.Values.GetValue(i));
                    }
                }
                client.Close();
            }

            //List<TimeSeries> TSList = new List<TimeSeries>();
            //using (HnMapEntities databes = new HnMapEntities())
            //{
            //    var queryCityResult = from cities in databes.市界_REGION
            //                          select cities.NAME;
            //    List<string> CityList = new List<string>();
            //    CityList = queryCityResult.ToList();
            //    foreach (var city in CityList)
            //    {
            //        List<DateTime> dt = new List<DateTime>();
            //        List<Decimal> values = new List<Decimal>();
            //        var queryGDPResult = from gdp in databes.GDPTEST
            //                             where gdp.CITYNAME.Equals(city)
            //                             select gdp;
            //       // Console.WriteLine(queryGDPResult.Count());
            //        if (queryGDPResult.Count() > 0)
            //        {

            //            foreach (var gdpitem in queryGDPResult.ToList())
            //            {
            //                //Console.WriteLine("{0}'GDP in {1} is : {2}", city, gdpitem.TIME, gdpitem.GDPVALUE);
            //                dt.Add(gdpitem.TIME);
            //                values.Add(gdpitem.GDPVALUE);
            //            }
            //            TSList.Add(TimeSeries.GetTSFromList(city, dt, values, false));
            //        }

            //    }
            //    foreach (var item in TSList)
            //    {
            //        //Console.WriteLine(item.Title);
            //        for (int i = 0; i < item.Count; i++)
            //        {
            //            Console.WriteLine("{0} in {1} is: {2}", item.Title, item.TimeStamp(i), item.ValueAtTime(item.TimeStamp(i)));
            //        }
            //    }
            }
            //TimeSeries ts = new TimeSeries();
            //List<DateTime> dt = new List<DateTime>();
            //List<double> values = new List<double>();
            //using (StreamReader sr = new StreamReader("c:\\table.csv"))
            //{
            //    List<TimeSeries> lsTS = new List<TimeSeries>();
            //    lsTS = TimeSeries.GetTSFromReader(sr, false);
            //    //Console.WriteLine(lsTS.Count);
            //    ts = lsTS.First();
            //    Console.WriteLine("=========================构造时间序列完成，下面输出时间序列============================");
            //    //Console.WriteLine("时间序列的自相关系数： {0}", ts.ComputeACF(ts.Count, true));
            //    //Console.WriteLine("时间序列的偏相关系数 : {0}", TimeSeries.GetPACFFrom(ts.ComputeACF(ts.Count, true)));
            //    //for (int i = 0; i < ts.Count; i++)
            //    //{
            //    //    Console.WriteLine("{0} : {1}", ts.TimeStamp(i), ts.ValueAtTime(ts.TimeStamp(i)));
            //    //}
            //    //Console.WriteLine(TimeSeries.GetPACFFrom(lts.ComputeACF(lts.Count, false)).ToString());
            //    LogReturnTransformation lrTransform = new LogReturnTransformation();
            //    StringBuilder strBulider = new StringBuilder();
            //    lrTransform.SetInput(0, ts, strBulider);
            //    TimeSeries transformedTS = new TimeSeries();
            //    lrTransform.Recompute();
            //    transformedTS = lrTransform.GetOutput(0) as TimeSeries;
            //    Console.WriteLine("=========================时间序列进行Log-Return变换完成，下面输出变换后时间序列============================");
            //    //Console.WriteLine("Log-Return时间序列的自相关系数： {0}", transformedTS.ComputeACF(transformedTS.Count, true));
            //    //Console.WriteLine("Log-Return时间序列的偏相关系数 : {0}", TimeSeries.GetPACFFrom(transformedTS.ComputeACF(transformedTS.Count, true)));
            //    //for (int i = 0; i < transformedTS.Count; i++)
            //    //{
            //    //    Console.WriteLine("{0} : {1}", transformedTS.TimeStamp(i), transformedTS.ValueAtTime(transformedTS.TimeStamp(i)));
            //    //}
            //    ARMAModel model = new ARMAModel(2, 2);
            //    model.SetInput(0, transformedTS, strBulider);
            //    TimeSeries modelTS = new TimeSeries();
            //    //model.TheData = transformedTS;
            //    model.FitByMLE(250, 250, 0, null);
            //    Console.WriteLine(model.Description);
            //    //Console.WriteLine(mod);
            //    // now do some forecasting beyond the end of the data
            //    var forecaster = new ForecastTransform();

            //    var futureTimes = new List<DateTime>();
            //    var nextTime = transformedTS.GetLastTime();
            //    for (int t = 0; t < 3; ++t)                    // go eight days into the future beyond the end of the data we have
            //    {
            //        nextTime = nextTime.AddDays(1);
            //        futureTimes.Add(nextTime);
            //    }
            //    forecaster.FutureTimes = futureTimes.ToArray(); // these are future times at which we want forecasts
            //    // for now, ARMA models do not use the times in any meaningful way when forecasting,
            //    // they just assume that these are the timestamps for the next sequential points
            //    // after the end of the existing time series

            //    forecaster.SetInput(0, model, null);            // the ARMA model used for forecasting
            //    forecaster.SetInput(1, transformedTS, null);    // the original data

            //    // normally you would call the Recompute() method of a transform, but there is no need
            //    // here; it is automatically called as soon as all inputs are set to valid values (in the 2 statements above)
            //    var predictors = forecaster.GetOutput(0) as TimeSeries;

            //    // now predictors is a time series of the forecast values for the next 8 days
            //    // that is, predictors[0] is the predictive mean of X_{101} given X_1,...,X_100,
            //    //          predictors[1] is the predictive mean of X_{102} given X_1,...,X_100, etc.
            //    for (int i = 0; i < predictors.Count; i++)
            //    {
            //        Console.WriteLine("{0} : {1}", predictors.TimeStamp(i), Math.Exp(predictors.ValueAtTime(predictors.TimeStamp(i)) + Math.Log(ts.ValueAtTime(ts.GetLastTime()))));
            //    }
            //}
            //using (DEMOEntities database = new DEMOEntities()) 
            //{
            //    var queryResult = from gdp in database.Table_GDP
            //                      select gdp.Datetime;
            //    dt = queryResult.ToList();

            //    var queryResult1 = from gdp in database.Table_GDP
            //                      select gdp.Value;
            //    for (int i = 0; i < queryResult1.ToList().Count; i++)
            //    {
            //        values.Add(Convert.ToDouble(queryResult1.ToList()[i]));
            //    }

            //    Console.WriteLine("=======================数据读入完成，下面输出读入的数据==============================");
            //    for (int i = 0; i < dt.Count; i++)
            //    {
            //        Console.WriteLine("{0} : {1}", dt[i], values[i]);
            //    }
            //    ts = TimeSeries.GetTSFromList(dt, values, false);
            //    Console.WriteLine("=========================构造时间序列完成，下面输出时间序列============================");
            //    Console.WriteLine("时间序列的自相关系数： {0}", ts.ComputeACF(ts.Count, true));
            //    Console.WriteLine("时间序列的偏相关系数 : {0}", TimeSeries.GetPACFFrom(ts.ComputeACF(ts.Count, true)));
            //    for (int i = 0; i < ts.Count; i++)
            //    {
            //        Console.WriteLine("{0} : {1}", ts.TimeStamp(i), ts.ValueAtTime(ts.TimeStamp(i)));
            //    }
            //    //Console.WriteLine(TimeSeries.GetPACFFrom(lts.ComputeACF(lts.Count, false)).ToString());
            //    LogReturnTransformation lrTransform = new LogReturnTransformation();
            //    StringBuilder strBulider = new StringBuilder();
            //    lrTransform.SetInput(0, ts, strBulider);
            //    TimeSeries transformedTS = new TimeSeries();
            //    lrTransform.Recompute();
            //    transformedTS = lrTransform.GetOutput(0) as TimeSeries;
            //    Console.WriteLine("=========================时间序列进行Log-Return变换完成，下面输出变换后时间序列============================");
            //    Console.WriteLine("Log-Return时间序列的自相关系数： {0}", transformedTS.ComputeACF(transformedTS.Count, true));
            //    Console.WriteLine("Log-Return时间序列的偏相关系数 : {0}", TimeSeries.GetPACFFrom(transformedTS.ComputeACF(transformedTS.Count, true)));
            //    for (int i = 0; i < transformedTS.Count; i++)
            //    {
            //        Console.WriteLine("{0} : {1}",transformedTS.TimeStamp(i), transformedTS.ValueAtTime(transformedTS.TimeStamp(i)));
            //    }
            //    ARMAModel model = new ARMAModel(2, 2);
            //    model.SetInput(0, transformedTS, strBulider);
            //    TimeSeries modelTS = new TimeSeries();
            //    //model.TheData = transformedTS;
            //    model.FitByMLE(250, 250, 0, null);
            //    Console.WriteLine(model.Description);
            //    //Console.WriteLine(mod);
            //    // now do some forecasting beyond the end of the data
            //    var forecaster = new ForecastTransform();

            //    var futureTimes = new List<DateTime>();
            //    var nextTime = transformedTS.GetLastTime();
            //    for (int t = 0; t < 3; ++t)                    // go eight days into the future beyond the end of the data we have
            //    {
            //        nextTime = nextTime.AddDays(1);
            //        futureTimes.Add(nextTime);
            //    }
            //    forecaster.FutureTimes = futureTimes.ToArray(); // these are future times at which we want forecasts
            //    // for now, ARMA models do not use the times in any meaningful way when forecasting,
            //    // they just assume that these are the timestamps for the next sequential points
            //    // after the end of the existing time series

            //    forecaster.SetInput(0, model, null);            // the ARMA model used for forecasting
            //    forecaster.SetInput(1, transformedTS, null);    // the original data

            //    // normally you would call the Recompute() method of a transform, but there is no need
            //    // here; it is automatically called as soon as all inputs are set to valid values (in the 2 statements above)
            //    var predictors = forecaster.GetOutput(0) as TimeSeries;

            //    // now predictors is a time series of the forecast values for the next 8 days
            //    // that is, predictors[0] is the predictive mean of X_{101} given X_1,...,X_100,
            //    //          predictors[1] is the predictive mean of X_{102} given X_1,...,X_100, etc.
            //    for (int i = 0; i < predictors.Count; i++)
            //    {
            //        Console.WriteLine("{0} : {1}", predictors.TimeStamp(i), Math.Exp(predictors.ValueAtTime(predictors.TimeStamp(i)) + Math.Log(ts.ValueAtTime(ts.GetLastTime()))));
            //    }

            //    //Console.WriteLine(Math.Exp(predictors));

            //}
        }

    }


